package com.jx.flowershopmanager.home;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;

import com.jx.flowershopmanager.R;
import com.jx.flowershopmanager.base.BaseActivity;
import com.jx.flowershopmanager.home.fragment.IndexFragment;
import com.jx.flowershopmanager.home.fragment.OrderFragment;
import com.jx.flowershopmanager.home.fragment.ProfileFragment;
import com.jx.flowershopmanager.home.fragment.StorehouseFragment;
import com.jx.flowershopmanager.view.TabView;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends BaseActivity {

    private static final int INDEX_HOME = 0;
    private static final int INDEX_ORDER = 1;
    private static final int INDEX_PROFILE = 2;

    private final List<TabView> mTabViews = new ArrayList<>();

    private  ViewPager mViewPager;
    /// 首页
    private TabView mIndexTabView;
    /// 订单
    private TabView mOrderTabView;
    /// 我的
    private TabView mProfileTabView;

    String[] mTabTitles = {"首页","仓库","订单","我的"};

    @Override
    protected int layoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected void initView() {
        mViewPager = findViewById(R.id.viewpager);
        mIndexTabView = findViewById(R.id.tab_weixin);
        mOrderTabView = findViewById(R.id.tab_find);
        mProfileTabView = findViewById(R.id.tab_profile);

        mIndexTabView.setOnClickListener(v -> {
            mViewPager.setCurrentItem(0);
        });
        mOrderTabView.setOnClickListener(v -> {
            mViewPager.setCurrentItem(1);
        });
        mProfileTabView.setOnClickListener(v -> {
            mViewPager.setCurrentItem(2);
        });
    }

    @Override
    protected void initData() {
        mTabViews.add(mIndexTabView);
        mTabViews.add(mOrderTabView);
        mTabViews.add(mProfileTabView);

        mViewPager.setOffscreenPageLimit(mTabTitles.length - 1);
        mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            /**
             * @param position 滑动的时候，position总是代表左边的View， position+1总是代表右边的View
             * @param positionOffset 左边View位移的比例
             * @param positionOffsetPixels 左边View位移的像素
             */
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // 左边View进行动画
                mTabViews.get(position).setXPercentage(1 - positionOffset);
                // 如果positionOffset非0，那么就代表右边的View可见，也就说明需要对右边的View进行动画
                if (positionOffset > 0) {
                    mTabViews.get(position + 1).setXPercentage(positionOffset);
                }
            }
        });
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int i) {
            return getTabFragment(i, mTabTitles[i]);
        }

        @Override
        public int getCount() {
            return mTabTitles.length;
        }
    }


    private Fragment getTabFragment(int index, String title) {
        Fragment fragment = new IndexFragment();
        if (index == INDEX_HOME){
            fragment = new IndexFragment();
        }else if(index == INDEX_ORDER){
            fragment = new OrderFragment();
        }else  if(index == INDEX_PROFILE){
            fragment = new ProfileFragment();
        }
        return fragment;
    }

}